/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLElement;

public class IdentifyRegistration implements IElement {


    public static final String NAME = "IdentifyRegistration";

    private INode parentNode;

    private Identify identify = null;
    private LinkedList<CorrectnessCondition> correctnessConditionList = new LinkedList<CorrectnessCondition>();
    private Correctness correctness = null;


    private IdentifyRegistration(INode parentNode){
        this.parentNode = parentNode;
    }

    public INode getParentNode() {
        return parentNode;
    }

    public static IdentifyRegistration newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        IdentifyRegistration identifyRegistration = new IdentifyRegistration(parentNode);
        XMLElement xmlElement = xmlElementList.poll();
        if (!xmlElement.getName().equals(NAME)){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a " + NAME, xmlElementList);
        }

        if (!xmlElement.isEmpty()){
            while (true){
                xmlElement = xmlElementList.peek();
                if (xmlElement == null){
                    throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
                }
                if (xmlElement.getName().equals(NAME) && xmlElement.isEndElement()){
                    xmlElement = xmlElementList.poll();
                    break;
                }
                if (xmlElement.getName().equals("Identify")){
                    if (identifyRegistration.identify == null) {
                        identifyRegistration.identify = Identify.newInstance(identifyRegistration, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Identify> in element " + NAME, xmlElementList);
                    }
                } else if (CorrectnessCondition.isCorrectnessCondition(xmlElement.getName())){
                    identifyRegistration.correctnessConditionList.add(CorrectnessCondition.newInstance(identifyRegistration, xmlElementList));
                } else if (xmlElement.getName().equals("Correctness")){
                    if (identifyRegistration.correctness == null) {
                        identifyRegistration.correctness = Correctness.newInstance(identifyRegistration, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Correctness> in element " + NAME, xmlElementList);
                    }
                } else 
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
            }
        }

        return identifyRegistration;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        XMLElement xmlElement = new XMLElement(NAME);
        boolean isEmpty = !(identify != null
         || !correctnessConditionList.isEmpty() || correctness != null);
        xmlElement.setEmpty(isEmpty);
        xmlElementList.add(xmlElement);
        if (this.identify != null){
            xmlElementList.addAll(this.identify.getXMLElementList());
        }
        for (CorrectnessCondition correctnessCondition : this.correctnessConditionList){
            xmlElementList.addAll(correctnessCondition.getXMLElementList());
        }
        if (this.correctness != null){
            xmlElementList.addAll(this.correctness.getXMLElementList());
        }
        if (!isEmpty) { 
            xmlElement = new XMLElement(NAME);
            xmlElement.setEndElement(true);
            xmlElementList.add(xmlElement);
        }
        return xmlElementList;
    }

    public Identify getIdentify(){
        return identify;
    }

    public LinkedList<CorrectnessCondition> getCorrectnessConditionList(){
        return correctnessConditionList;
    }

    public Correctness getCorrectness(){
        return correctness;
    }

	@Override
	public void accept(Visitor visitor) {
		if (visitor.visit(this)) {
			for(CorrectnessCondition correctnessCondition: this.correctnessConditionList) {
				correctnessCondition.accept(visitor);
			}
		}
		visitor.endVisit(this);
	}

	@Override
	public String toString(){
		ToStringVisitor toStringVisitor = new ToStringVisitor();
		this.accept(toStringVisitor);
		return toStringVisitor.toString();
	}

}
